<?php

namespace App\Http\Controllers\Admin;

use App\Model\Branch;
use App\Model\BranchTest;
use App\Model\tel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TelChargeController extends Controller
{
//    private $key = '055c7fbbf0021023adad7be38942c477';
    //创建话费订单  isok 0.未处理 1.成功 2.失败 3.充值中
    public function add(Request $request){
        $data = $request -> all();
        $secret = (new BranchTest)->getSecret($data['key']);
        if(!isset($secret)){
            return callBack(1002);
        }
        //检测权限
        if(!(isset($data['key']) && isset($data['goods']) && isset($data['orderno']) && isset($data['type']) && isset($data['chargeaccount']) && isset($data['buytime']) && isset($data['sign']))){
            return callBack(1004);
        }
        if((new BranchTest)->tel($data['key'],$secret) == 1){
            $data['secret'] = $secret;
//            return checkSign($data);
            if(checkSign($data)){
                if((new tel)->tel_exists($data['orderno']) == false){
                    $data['order'] = date('Ymd').str_pad(mt_rand(1,99999),5,'0',STR_PAD_LEFT);
                    $data['time'] = time();
                    $data['status'] = '未处理';
                    $data['num'] = 1;
                    $data['bid'] = (new BranchTest)->getId($data['key'],$secret);
                    switch ($data['goods']){
                        case "goods_101":
                            $data['price'] = 50;
                            break;
                        case "goods_102":
                            $data['price'] = 100;
                            break;
                        case "goods_103":
                            $data['price'] = 200;
                            break;
                        case "goods_104":
                            $data['price'] = 300;
                            break;
                        case "goods_105":
                            $data['price'] = 500;
                            break;
                        case "goods_106":
                            $data['price'] = 30;
                            break;
                        default:
                            $data['price'] = 0;
                            break;
                    }
                    $res = tel::create($data);
                    if($res){
                        $result = [
                            "order"              =>      $data['order'],
                            "orderno"            =>      $data['orderno'],
                            "chargeaccount"      =>      $data['chargeaccount'],
                            "type"               =>      $data['type'],
                            "goods"              =>      $data['goods'],
                            "time"               =>      $data['time'],
                        ];
                        $str = implode('',$result);
                        $result['sign'] = md5($data['key'].$str.$data['secret']);
                        return callBack(0,$result);
                    }
                }else{
                    return callBack(1003);
                }
            }else{
                return callBack(1001);
            }
        }else{
            return callBack(1002);
        }
    }

    //主动查询订单状态
    public function getOrderInfo(Request $request){
        $data = $request->all();
        $secret = (new BranchTest)->getSecret($data['key']);
        //检测权限
        if(!(isset($data['key']) && isset($data['orderno']) && isset($data['chargeaccount']) && isset($data['sign']))){
            return callBack(1004);
        }

        if((new BranchTest)->tel($data['key'],$secret) == 1){
            $data['secret'] = $secret;
//            return checkSignGetOrderInfo($data);
            if(checkSignGetOrderInfo($data)){
                $res = (new tel)->getOrderInfo($data['orderno']);
                if($res){
                    $result = [
                        "orderno"            =>      $data['orderno'],
                        "chargeaccount"      =>      $data['chargeaccount'],
                        "status"             =>      $res['isok'],
                        "finish_time"        =>      $res['oktime'],
                    ];
                    $str = implode('',$result);
                    $result['sign'] = md5($data['key'].$str.$data['secret']);
                    return callBack(200,$result);
                }else{
                    return callBack(1005);
                }
            }else{
                return callBack(1001);
            }
        }else{
            return callBack(1002);
        }
    }

    //测试回调接口
    public function addTest(Request $request){
        $data = $request -> all();
        $secret = (new BranchTest)->getSecret($data['key']);
        if(!isset($secret)){
            return callBack(1002);
        }
        //检测权限
        if(!(isset($data['key']) && isset($data['goods']) && isset($data['orderno']) && isset($data['type']) && isset($data['chargeaccount']) && isset($data['buytime']) && isset($data['sign']))){
            return callBack(1004);
        }
        if((new BranchTest)->tel($data['key'],$secret) == 1){
            $data['secret'] = $secret;
//            return checkSign($data);
            if(checkSign($data)){
                if((new tel)->tel_exists($data['orderno']) == false){
                    $data['order'] = date('Ymd').str_pad(mt_rand(1,99999),5,'0',STR_PAD_LEFT);
                    $data['time'] = time();
                    $data['status'] = '未处理';
                    $data['isok'] = 2;
                    $data['bid'] = (new BranchTest)->getId($data['key'],$secret);
                    switch ($data['goods']){
                        case "goods_101":
                            $data['price'] = 50;
                            break;
                        case "goods_102":
                            $data['price'] = 100;
                            break;
                        case "goods_103":
                            $data['price'] = 200;
                            break;
                        case "goods_104":
                            $data['price'] = 300;
                            break;
                        case "goods_105":
                            $data['price'] = 500;
                            break;
                        default:
                            $data['price'] = 0;
                            break;
                    }
                    $res = tel::create($data);
                    if($res){
                        $result = [
                            "order"              =>      $data['order'],
                            "orderno"            =>      $data['orderno'],
                            "chargeaccount"      =>      $data['chargeaccount'],
                            "type"               =>      $data['type'],
                            "goods"              =>      $data['goods'],
                            "time"               =>      $data['time'],
                        ];
                        $str = implode('',$result);
                        $result['sign'] = md5($data['key'].$str.$data['secret']);
                        return callBack(0,$result);
                    }
                }else{
                    return callBack(1003);
                }
            }else{
                return callBack(1001);
            }
        }else{
            return callBack(1002);
        }
    }

    //余额查询
    public function getBalance(Request $request){
        $data = $request -> all();
        $secret = (new BranchTest)->getSecret($data['key']);
        $id = (new BranchTest)->getId($data['key'],$secret);
        $res = Branch::where("id",$id)->first();
        if($res){
            return $res->balance;
        }else{
            return "KEY不正确";
        }
    }
}
